package avicit.bdp.dms.das.rest;

import avicit.bdp.dms.das.dto.ColumnReservedDTO;
import avicit.bdp.dms.das.dto.ColumnTypeDTO;
import avicit.bdp.dms.das.service.ColumnReservedService;
import avicit.bdp.dms.das.service.ColumnTypeService;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
 * 模型字段管理REST接口
 */
@RestController
@Api(tags = "模型字段管理REST接口")
@RequestMapping("/api/bdp/dms/das/column")
public class ColumnTypeRest {

    @Autowired
    private ColumnTypeService columnTypeService;

    @Autowired
    private ColumnReservedService columnReservedService;


    /**
     * 根据数据库类型查询字段类型
     */
    @ApiOperation(value = "根据数据库类型查询字段类型")
    @ApiImplicitParam(value = "数据库类型", name = "type", dataType = "String")
    @GetMapping("/getColumnTypeListByType")
    public ResponseMsg<List<ColumnTypeDTO>> getColumnTypeListByType(@RequestParam(required = false) String type) {
        ResponseMsg<List<ColumnTypeDTO>> responseMsg = new ResponseMsg<>();
        List<ColumnTypeDTO> result = columnTypeService.getColumnTypeListByType(type);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 获取表的限制信息
     */
    @ApiOperation(value = "获取表的限制信息")
    @ApiImplicitParam(value = "数据库类型", name = "type", dataType = "String")
    @GetMapping("/getTableInfoByType")
    public ResponseMsg<Map<String, Object>> getTableInfoByType(@RequestParam(required = false) String type) {
        ResponseMsg<Map<String, Object>> responseMsg = new ResponseMsg<>();
        Map<String, Object> result = columnTypeService.getTableInfoByType(type);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    @ApiOperation(value = "检查是否使用保留字")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "数据库类型", name = "type", dataType = "String"),
            @ApiImplicitParam(value = "保留字", name = "keyword", dataType = "String")
    })
    @GetMapping("/checkColumnReserved")
    public ResponseMsg<Boolean> checkColumnReserved(@RequestParam(required = false) String type,
                                                    @RequestParam(required = false) String keyword) {
        ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
        boolean result = columnReservedService.checkColumnReserved(type, keyword);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    @ApiOperation(value = "获取数据源类型列表")
    @GetMapping("/getDataSourceTypeList")
    public ResponseMsg<List<ColumnReservedDTO>> getDataSourceTypeList() {
        ResponseMsg<List<ColumnReservedDTO>> responseMsg = new ResponseMsg<>();
        List<ColumnReservedDTO> result = columnReservedService.selectListAll();
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

}